package com.wlhse.util;

import com.wlhse.dao.CompanyDao;
import com.wlhse.dao.EmployeeManagementDao;
import com.wlhse.dto.EmployeeExcelDto;
import com.wlhse.entity.EmployeePojo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Component
class PoiEmploy {

    @Resource
    private CompanyDao companyDao;

    @Resource
    private EmployeeManagementDao employeeManagementDao;

    @Resource
    private DeleteCacheUtil deleteCacheUtil;

    void poiPoiEmploy(List<EmployeeExcelDto> list) {
        List<EmployeePojo> list1 = new ArrayList<>();
        for (EmployeeExcelDto dto : list) {
            //companyName
            String hash = HashUtil.hash(dto.getEmployeeCode() + dto.getName() + dto.getSecondCompany() + dto.getThirdCompany());
            if (employeeManagementDao.queryByHash(hash) == 0) {
                EmployeePojo employeePojo = new EmployeePojo();
                String employeeCode = dto.getEmployeeCode();
                String name = dto.getName();
                String sex = dto.getSex();
                String birthDay = dto.getBirthDay();
                String joinClubTime = dto.getJoinClubTime();
                String employeeGroup = dto.getEmployeeGroup();
                String job = dto.getJob();
                String study = dto.getStudy();
                String secondCompany = dto.getSecondCompany();
                String thirdCompany = dto.getThirdCompany();

                String s = companyDao.queryByCompanyName(secondCompany);
                String companyCode = companyDao.getCompanyCode(s, thirdCompany);

                if (StringUtils.isNotBlank(birthDay)) {
                    employeePojo.setBirthday(birthDay);

                } else {
                    employeePojo.setBirthday("1990-1-1");
                }

                employeePojo.setCompanyCode(companyCode);
                employeePojo.setEducation(study);
                employeePojo.setEmail("");
                employeePojo.setEmpGroup(employeeGroup);
                employeePojo.setEmployeeCode(employeeCode);
                if (StringUtils.isNotBlank(joinClubTime)) {
                    employeePojo.setJobTime(joinClubTime);
                } else {
                    employeePojo.setJobTime("2000-1-1");
                }
                employeePojo.setName(name);
                employeePojo.setPosition("");
                employeePojo.setSex(sex);
                employeePojo.setStation(job);
                employeePojo.setTel("");

                employeePojo.setHash(hash);

                list1.add(employeePojo);
            }
        }
        if (list1.size() > 0) {
            employeeManagementDao.addManyEmployeePojo(list1);
            deleteCacheUtil.deleteEmployeesCache();
        }
    }
}
